package presentation.control;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import presentation.data.LoginData;
import presentation.gui.EbazaarMainFrame;
import presentation.gui.LoginWindow;
import business.Login;
import business.SessionCache;
import business.exceptions.BackendException;
import business.exceptions.UserException;
import business.externalinterfaces.CustomerConstants;

public class LoginUIControl {
	//private SessionContext context;
    private LoginWindow loginWindow;
    private Callback controller;
    private LoginData loginData = new LoginData();
    
    
    Component currWindow;
    Component parentWindow;
    
    //If parent and current windows are same, we don't make one vanish
    //while the other is displayed.
    private boolean parentIsOuterFrame;
    
    public LoginUIControl(Component currWindow, Component parentWindow){
        this.currWindow = currWindow;
        this.parentWindow = parentWindow;
        parentIsOuterFrame = (parentWindow.getClass() == EbazaarMainFrame.class);
    }
    public LoginUIControl(Component currWindow, Component parentWindow, Callback controller){
        this(currWindow,parentWindow);
        this.controller=controller;
    }    
    
    public void startLogin() {
        loginWindow = new LoginWindow(this); 
        EbazaarMainFrame.getInstance().getDesktop().add(loginWindow);
        if(!parentIsOuterFrame) parentWindow.setVisible(false);
        loginWindow.setVisible(true);
    }
    
    //////// event handling code
    
    public SubmitListener getSubmitListener(LoginWindow w) {
        return new SubmitListener();
    }
    public CancelListener getCancelListener(LoginWindow w) {
        return new CancelListener();
    }
    
	class SubmitListener implements ActionListener {
		
        public void actionPerformed(ActionEvent evt) {
            loginWindow.setVisible(false);
            Login login = loginData.extractLogin(loginWindow);
            boolean loginSuccessful = false;
            try {
            	int authorizationLevel = loginData.authenticate(login);
            	loginSuccessful = true;
            	loginData.loadCustomer(login, authorizationLevel);
            } catch(UserException e) {
            	JOptionPane.showMessageDialog(loginWindow,                                                    
                        "Error: "+e.getMessage(),
                        "Error", 
                        JOptionPane.ERROR_MESSAGE);
            	loginWindow.setVisible(true);
            } catch(BackendException e) {
            	JOptionPane.showMessageDialog(loginWindow,                                                    
                        "Error: "+e.getMessage(),
                        "Error", 
                        JOptionPane.ERROR_MESSAGE);
            	parentWindow.setVisible(true);
            	loginWindow.dispose();
            }
            if(loginSuccessful) {
            	JOptionPane.showMessageDialog(loginWindow,                                                    
                    "Login successful",
                    "Success", 
                    JOptionPane.INFORMATION_MESSAGE);   
            }
       	    
            loginWindow.dispose();
       	    if(controller != null){
       	    	Boolean loggedIn = (Boolean)SessionCache.getInstance().get(CustomerConstants.LOGGED_IN);
       	    	if(loggedIn==Boolean.TRUE) controller.doUpdate();
       	    	else parentWindow.setVisible(true);
       	    }
       	    else {
       	    	Boolean loggedIn = (Boolean)SessionCache.getInstance().get(CustomerConstants.LOGGED_IN);
       	    	if(loggedIn==Boolean.TRUE) currWindow.setVisible(true);
       	    	else parentWindow.setVisible(true);
       	    	
       	    }   	    
        }
	}
	class CancelListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
        	
        	if(parentWindow != null) {
        		parentWindow.setVisible(true);
        	}
        	loginWindow.dispose();

        }
	}
}
